using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
namespace DAL
{
    public class tbTTGiaoDichDAL
    {
        private SqlConnection cn = null;
        public tbTTGiaoDichDAL()
            
        {
             cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
             cn.Open();
        }

        public void Insert(tbTTGiaoDich ttdg)
        {
            SqlCommand cm = new SqlCommand("sp_TTGiaoDich_insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdTTGiaoDich ", ttdg.IdTTGiaoDich);
            cm.Parameters.AddWithValue("@IdDH", ttdg.IdDH);
            cm.Parameters.AddWithValue("@IdHinhThuc ", ttdg.IdHinhThuc);
            cm.Parameters.AddWithValue("@SoTien", ttdg.SoTien);
            cm.ExecuteNonQuery();
        }
        public void Update(tbTTGiaoDich ttdg)
        {
            SqlCommand cm = new SqlCommand("sp_TTGiaoDich_update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdTTGiaoDich ", ttdg.IdTTGiaoDich);
            cm.Parameters.AddWithValue("@IdDH", ttdg.IdDH);
            cm.Parameters.AddWithValue("@IdHinhThuc ", ttdg.IdHinhThuc);
            cm.Parameters.AddWithValue("@SoTien", ttdg.SoTien);
            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("sp_TTGiaoDich_delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdTTGiaoDich", Id);
            cm.ExecuteNonQuery();
        }
        public  tbTTGiaoDichCollection Get(string Where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_TTGiaoDich_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbTTGiaoDichCollection ttdgcollection = new tbTTGiaoDichCollection();
            while (dr.Read())
            {
                tbTTGiaoDich ttdg = new tbTTGiaoDich();
                ttdg.IdTTGiaoDich = int.Parse(dr.GetValue(0).ToString());
                ttdg.IdDH = int.Parse(dr.GetValue(1).ToString());
                ttdg.IdHinhThuc = int.Parse(dr.GetValue(2).ToString());
                ttdg.SoTien = int.Parse(dr.GetValue(3).ToString());
                ttdgcollection.Add(ttdg);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ttdgcollection;

        }
    }
}
